package org.example.interceptor;

import com.alibaba.druid.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.utils.JwtHelper;
import org.example.utils.Result;
import org.example.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class LoginProtectInterceptor implements HandlerInterceptor {
   @Autowired
   private JwtHelper jwtHelper;

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
     String token = request.getHeader("token");
     if (StringUtils.isEmpty(token) || jwtHelper.isExpiration(token)) {
       Result<Object> result = Result.build(null, ResultCodeEnum.NOTLOGIN);
       ObjectMapper mapper = new ObjectMapper();
       String json = mapper.writeValueAsString(result);
       response.getWriter().print(json);
       return false;
     }
     return true;
  }
}
